﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Sublimation.Behaviours
{
    public class EnemyUnitGoalBehaviour : Behaviour
    {
        protected WorldProperties worldProperties;
        protected EnemyUnit owner;
        protected float weight;
        public float getWeight() { return this.weight; }

        public EnemyUnitGoalBehaviour(WorldProperties worldProperties, EnemyUnit owner, float weight)
        {
            this.worldProperties = worldProperties;
            this.owner = owner;
            this.weight = weight;
        }

        public Vector2 RunBehaviour()
        {
            // assign a goal to the enemy unit - currently not worrying about sight range here (towers are big!)

            Vector2 result = Vector2.Zero;

            // originally i thought about having them factor in the direction of all towers with a stronger weight for the closest, but then i realised that would be strange. Since it's like gravity you could end up trapping them.
            float distanceToClosestTower = 0.0f;
            foreach (Player player in worldProperties.Players)
            {
                foreach (Tower tower in player.Towers)
                {
                    Vector2 vectorToTower = (tower.Position - owner.Position); //XXX: should we bother taking into account origin or nearest edge? like collision code
                    if (result == Vector2.Zero || vectorToTower.Length() < distanceToClosestTower)
                    {
                        distanceToClosestTower = vectorToTower.Length();
                        result = vectorToTower;
                    }
                }
            }

            // should we try to move at max speed? - up to the distance anyway, in which case perhaps we shouldn't change this. but then that would throw it out in relation to other movements. Hmm.
            // lets clamp it for now
            if(distanceToClosestTower > owner.MaxSpeed) result *= owner.MaxSpeed / distanceToClosestTower;

            return result * weight;
        }
    }
}
